package com.glsc.ngateway.common.base.converter.platform;

import com.alibaba.excel.util.CollectionUtils;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.flow.FlowFormSystemNameApply;
import com.glsc.ngateway.common.base.domainmapper.FlowFormSystemNameApplyMapper;
import com.glsc.ngateway.common.api.flowable.dto.task.formData.system.domainDto.FlowFormSystemNameApplyDto;
import org.apache.logging.log4j.util.Strings;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author xuchang
 */
public class FlowFormSystemNameApplyConverter extends AbstractFlowFormDataConverter {

    public static FlowFormSystemNameApplyDto do2Dto(FlowFormSystemNameApply apply) {
        if(Objects.isNull(apply)){
            return null;
        }
        FlowFormSystemNameApplyDto applyDto = FlowFormSystemNameApplyMapper.INSTANCE.domain2dto(apply);

        applyDto.setBusinessDepartment(Strings.isBlank(apply.getBusinessDepartmentArr()) ? java.util.Collections.EMPTY_LIST
                : Arrays.stream(apply.getBusinessDepartmentArr().split(",")).collect(Collectors.toList()));

        applyDto.setOpGroupId(Strings.isBlank(apply.getOpGroupIdArr()) ? java.util.Collections.EMPTY_LIST
                : Arrays.stream(apply.getOpGroupIdArr().split(",")).collect(Collectors.toList()));

        applyDto.setMemberManager(Strings.isBlank(apply.getMemberManagerArr()) ? java.util.Collections.EMPTY_LIST
                : Arrays.stream(apply.getMemberManagerArr().split(",")).collect(Collectors.toList()));

        applyDto.setSystemManager(Strings.isBlank(apply.getSystemManagerArr()) ? java.util.Collections.EMPTY_LIST
                : Arrays.stream(apply.getSystemManagerArr().split(",")).collect(Collectors.toList()));

        applyDto.setOpsManager(Strings.isBlank(apply.getOpsManagerArr()) ? java.util.Collections.EMPTY_LIST
                : Arrays.stream(apply.getOpsManagerArr().split(",")).collect(Collectors.toList()));

        applyDto.setAppendicesFileList(converter2FileList(apply.getAppendicesFileIdList()));

        return applyDto;
    }

    public static List<FlowFormSystemNameApplyDto> do2Dto(List<FlowFormSystemNameApply> applyList) {
        return CollectionUtils.isEmpty(applyList) ? java.util.Collections.emptyList() : applyList.stream().map(FlowFormSystemNameApplyConverter::do2Dto).collect(Collectors.toList());
    }

    public static FlowFormSystemNameApply dto2Do(FlowFormSystemNameApplyDto applyDto) {
        if(Objects.isNull(applyDto)){
            return null;
        }

        FlowFormSystemNameApply apply = FlowFormSystemNameApplyMapper.INSTANCE.dto2domain(applyDto);

        apply.setBusinessDepartmentArr(CollectionUtils.isEmpty(applyDto.getBusinessDepartment()) ? Strings.EMPTY
                : String.join(",", applyDto.getBusinessDepartment()));

        apply.setOpGroupIdArr(CollectionUtils.isEmpty(applyDto.getOpGroupId()) ? Strings.EMPTY
                : String.join(",", applyDto.getOpGroupId()));

        apply.setMemberManagerArr(CollectionUtils.isEmpty(applyDto.getMemberManager()) ? Strings.EMPTY
                : String.join(",", applyDto.getMemberManager()));

        apply.setSystemManagerArr(CollectionUtils.isEmpty(applyDto.getSystemManager()) ? Strings.EMPTY
                : String.join(",", applyDto.getSystemManager()));

        apply.setOpsManagerArr(CollectionUtils.isEmpty(applyDto.getOpsManager()) ? Strings.EMPTY
                : String.join(",", applyDto.getOpsManager()));

        apply.setAppendicesFileIdList(converter2FileIdList(applyDto.getAppendicesFileList()));

        return apply;
    }

    public static List<FlowFormSystemNameApply> dto2Do(List<FlowFormSystemNameApplyDto> applyDtoList) {
        return CollectionUtils.isEmpty(applyDtoList) ? java.util.Collections.emptyList() : applyDtoList.stream().map(FlowFormSystemNameApplyConverter::dto2Do).collect(Collectors.toList());
    }
}
